package com.robot.backend.service.impl.robot;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.robot.backend.pojo.Robot;
import com.robot.backend.service.robot.robotCRUDService;
import com.robot.backend.mapper.RobotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class robotCRUDServiceImpl implements robotCRUDService{
    @Autowired
    RobotMapper robotMapper;
    @Override
    public String addRobot(String title, String description, String content,String lang) {
        Robot robot = new Robot();
        robot.setUserid(StpUtil.getLoginIdAsInt());
        robot.setTitle(title);
        robot.setDescription(description);
        robot.setContent(content);
        Date date = new Date();
        robot.setCreatetime(date);
        robot.setModifytime(date);
        robot.setLang(lang);
        robotMapper.insert(robot);
        return "success";
    }

    @Override
    public String updateRobot(int id,String title, String description, String content,String lang) {
        Robot robot = robotMapper.selectById(id);
        if(robot.getUserid() != StpUtil.getLoginIdAsInt()) return "error";
        robot.setTitle(title);
        robot.setDescription(description);
        robot.setContent(content);
        Date date = new Date();
        robot.setModifytime(date);
        robot.setLang(lang);
        robotMapper.updateById(robot);
        return "success";
    }

    @Override
    public String deleteRobot(int id) {
        robotMapper.deleteById(id);
        return "success";
    }

    @Override
    public JSONObject showRobot(int page, int size) {
        QueryWrapper<Robot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",StpUtil.getLoginIdAsInt());
        IPage<Robot> iPage = new Page<>(page,size);
        robotMapper.selectPage(iPage,null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robot",robotMapper.selectPage(iPage,queryWrapper).getRecords());
        jsonObject.put("length",robotMapper.selectCount(queryWrapper));
        return jsonObject;
    }

    @Override
    public List<Robot> getAllRobot() {
        int id =StpUtil.getLoginIdAsInt();
        QueryWrapper<Robot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        List<Robot> list = robotMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public String getRobotByUserID(int uid) {
        QueryWrapper<Robot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",uid);
        List<Robot> list = robotMapper.selectList(queryWrapper);
        return JSON.toJSONString(list);
    }
}
